﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using QLC.Modules.SCHEDULE.Components;
using ASPNETVN.PORTAL.Components.Security;

namespace QLC.Modules.SCHEDULE.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.Delete += AddEditItem_Delete;
            this.Update += AddEditItem_Update;

            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;

            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Item item = new Item();
            if (this.ItemID > 0)
            {
                item = Item.Load(this.ItemID);
            }

            item.Title = txtTitle.Text;
            item.Description = HttpUtility.HtmlEncode(txtHtmlContent.Value);
            item.ModuleID = this.ModuleID;

            if (this.ItemID == 0)
            {
                item.CreatedDate = DateTime.Now;
                item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                item.Insert();
            }
            else
            {
                item.Update();
            }

            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Item item = Item.Load(this.ItemID);
                if (item != null)
                {
                    txtTitle.Text = item.Title;
                    txtHtmlContent.Value = Server.HtmlDecode(item.Description);
                }
            }
        }
    }
}